package mipo.top.pet.controller;

import mipo.top.admin.domain.Employee;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.LoginContext;
import mipo.top.base.util.PageUtil;
import mipo.top.pet.domain.ProductSaleMsg;
import mipo.top.pet.query.ProductSaleMsgQuery;
import mipo.top.pet.Service.IProductSaleMsgService;
import mipo.top.users.domain.Logininfo;
import mipo.top.users.domain.User;
import mipo.top.users.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/productSaleMsg")
public class ProductSaleMsgController {

    @Autowired
    private IProductSaleMsgService productSaleMsgService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IUserService userService;

    @GetMapping
    public List<ProductSaleMsg> sad(){
        return productSaleMsgService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody ProductSaleMsg productSaleMsg){
        try {
            if(productSaleMsg.getId()==null){
                productSaleMsgService.save(productSaleMsg);
            }else{
                productSaleMsgService.update(productSaleMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        productSaleMsgService.remove(id);
    }

    @PostMapping
    public PageUtil<ProductSaleMsg> asp(@RequestBody ProductSaleMsgQuery productSaleMsgQuery){
        return productSaleMsgService.queryData(productSaleMsgQuery);
    }
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody ProductSaleMsg productSaleMsg , HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Long id = loginUser.getId();
            User user = userService.findlogininfoId(id);
            productSaleMsg.setUserId(user.getId());
            productSaleMsgService.publish(productSaleMsg);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        productSaleMsgService.patchremove(ids);
    }

    @PostMapping("/toAudit")
    public PageUtil<ProductSaleMsg> toAudit(@RequestBody ProductSaleMsgQuery productSaleMsgQuery){
        productSaleMsgQuery.setState(0);
        return productSaleMsgService.queryByStateData(productSaleMsgQuery);
    }

    @PostMapping("/toHandle")
    public PageUtil<ProductSaleMsg> toHandle(@RequestBody ProductSaleMsgQuery productSaleMsgQuery,HttpServletRequest request){
        try{
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            productSaleMsgQuery.setShopId(employee.getShopId());
            return productSaleMsgService.queryByStateData(productSaleMsgQuery);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/pool")
    public PageUtil<ProductSaleMsg> pool(@RequestBody ProductSaleMsgQuery productSaleMsgQuery){
        productSaleMsgQuery.setState(1);
        return productSaleMsgService.queryByStateData(productSaleMsgQuery);
    }
    @PostMapping("/finish")
    public PageUtil<ProductSaleMsg> finish(@RequestBody ProductSaleMsgQuery productSaleMsgQuery){
        return productSaleMsgService.queryByStateData(productSaleMsgQuery);
    }
    @PostMapping("/user")
    public PageUtil<ProductSaleMsg> pool(@RequestBody ProductSaleMsgQuery productSaleMsgQuery,HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            productSaleMsgQuery.setShopId(employee.getShopId());
            return productSaleMsgService.queryByStateData(productSaleMsgQuery);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}